import logging
import os
import shutil
import sys
import unittest

import mhtml
import util


__author__  = 'ZL <837806295@qq.com>'
__product__ = 'test'
__version__ = '3.2.0'
__date__    = '2023/04/08'


log = logging.getLogger(__product__)


class TestCase001(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.work_dir = util.get_test_subdir(__file__, cls.__name__)

    @classmethod
    def tearDownClass(cls):
        del cls.work_dir
        util.remove_test_subdir(__file__, cls.__name__)

    def setUp(self):
        self.work_dir = self.__class__.work_dir

    def tearDown(self):
        del self.work_dir

    def test_command(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" -h'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py')
                )
        )

        self.assertTrue(util.run(cmd))

class TestCase002(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.test_sequence = 0
        cls.work_dir = util.get_test_subdir(__file__, cls.__name__)

    @classmethod
    def tearDownClass(cls):
        del cls.test_sequence
        del cls.work_dir

        util.remove_test_subdir(__file__, cls.__name__)

    def setUp(self):
        self.test_sequence = self.__class__.test_sequence
        self.work_dir = self.__class__.work_dir
        self.test_dir = util.get_subdir(
            self.work_dir,
            ('{:0>%d}' % 3).format(self.test_sequence + 1))

        shutil.copytree(
            util.get_test_subdir(__file__, ['data', 'file']),
            os.path.join(self.test_dir, 'file'))

    def tearDown(self):
        del self.test_sequence
        del self.work_dir
        del self.test_dir

        self.__class__.test_sequence += 1

    def test_subcommand_image(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" image -h'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_image2(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" image -l debug "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    os.path.join(self.test_dir, 'file', 'case01.mht')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_image3(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" image -L image3 -o "%s" "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    os.path.join(self.test_dir),
                    os.path.join(self.test_dir, 'file', 'case02.mhtml')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_image4(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" image -l debug -L image4 "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    os.path.join(self.test_dir, 'file')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_image5(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" image -L image5 -o "%s" "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    os.path.join(self.test_dir),
                    os.path.join(self.test_dir, 'file')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_image6(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" image -L image6 -s -o "%s" "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    os.path.join(self.test_dir),
                    os.path.join(self.test_dir, 'file')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_image7(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" image -L image7 -s -n original -o "%s" "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    os.path.join(self.test_dir),
                    os.path.join(self.test_dir, 'file')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_image8(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" image -L image7+ -o "%s" "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    os.path.join(self.test_dir),
                    os.path.join(self.test_dir, 'file')
                )
        )

        self.assertFalse(util.run(cmd))

class TestCase101(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.work_dir = util.get_test_subdir(__file__, cls.__name__)

    @classmethod
    def tearDownClass(cls):
        del cls.work_dir
        util.remove_test_subdir(__file__, cls.__name__)

    def setUp(self):
        self.work_dir = self.__class__.work_dir

    def tearDown(self):
        del self.work_dir

class TestCase102(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.work_dir = util.get_test_subdir(__file__, cls.__name__)

    @classmethod
    def tearDownClass(cls):
        del cls.work_dir
        util.remove_test_subdir(__file__, cls.__name__)

    def setUp(self):
        self.work_dir = self.__class__.work_dir

    def tearDown(self):
        del self.work_dir

    def test_truncate(self):
        self.assertEqual(
            util.truncate('0123456789abcdef', 10),
            '0123456789')
        self.assertEqual(
            util.truncate('0123456789abcdef', 20),
            '0123456789abcdef')
        self.assertEqual(
            len(util.truncate('0123456789abcdef', 10, True)),
            43)
        self.assertEqual(
            util.truncate('', 10),
            '')

    def test_is_identifier_ascii(self):
        self.assertTrue(
            util.is_identifier_ascii(
                'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-.'))

        self.assertFalse(util.is_identifier_ascii(''))
        self.assertFalse(util.is_identifier_ascii('@'))
        self.assertFalse(util.is_identifier_ascii('abc@'))

    def test_has_extension2(self):
        self.assertTrue(
            util.has_extension2('abc.txt', ['.txt', '.xyz']))
        self.assertTrue(
            util.has_extension2('abc.TXT', ['.txt', '.xyz']))
        self.assertTrue(
            util.has_extension2('/abc/abc.txt', ['txt', 'xyz']))
        self.assertTrue(
            util.has_extension2('.txt', ['.txt', '.xyz']))

        self.assertFalse(
            util.has_extension2('abc.txt', '.txt'))
        self.assertFalse(
            util.has_extension2('abc.txt', []))
        self.assertFalse(
            util.has_extension2('abc.txt', [8]))
        self.assertFalse(
            util.has_extension2('abc.txt', ['.xyz']))

if __name__ == '__main__':
    util.set_log(__file__, '%s_v%s' % (__product__, __version__), 'debug')

    log.info('program version: %s %s', __product__, __version__)
    log.debug('command line: %s', sys.argv)

    unittest.main()
